<template>
	<section>
		<!--工具条高级查询框-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="search">搜索查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="标识" width="120" sortable>
			</el-table-column>
      <el-table-column prop="businessName" label="商机名字" width="150" sortable>
      </el-table-column>
      <el-table-column prop="productName" label="产品名字" width="150" sortable>
      </el-table-column>
			<el-table-column prop="price" label="产品价格" width="150" sortable>
			</el-table-column>
			<el-table-column prop="customerName" label="顾客姓名" width="150" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="180" sortable>
          <template slot-scope="scope">
              <span v-if="scope.row.state == 0" style="color: black">待付款</span>
              <span v-if="scope.row.state == 1" style="color: green">已付定金</span>
              <span v-if="scope.row.state == 2" style="color: cyan">已付尾款</span>
              <span v-if="scope.row.state == 3" style="color: pink">已完成</span>
              <span v-if="scope.row.state == -1" style="color: red">取消</span>
          </template>
      </el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button v-if="scope.row.state == 1"  type="danger" size="small" @click="handlePayOrder(scope.$index, scope.row)">付尾款</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条批量删除分页-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchDelete" >批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     :current-page="query.currentPage"
                     @current-change="handleCurrentChange"
                     :page-size="query.pageSize"
                     :total="pageInfo.total"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
	  <el-dialog title="新增/修改" :visible.sync="orderFormVisible" :close-on-click-modal="false">
			<el-form :model="orderForm" label-width="80px" :rules="orderFormRules" ref="orderForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="orderForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="部门经理" >
          <el-select v-model="orderForm.manager" clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
            </el-option>
          </el-select>
        </el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="orderForm.state">
						<el-radio class="radio" :label="1">正常</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
        </el-form-item>
          <el-form-item label="简介" >
            <el-input type="textarea" v-model="orderForm.intro" auto-complete="off"></el-input>
          </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="orderFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="orderFormLoading">提交</el-button>
			</div>
		</el-dialog>


    <!--付尾款弹框-->
    <el-dialog title="支付尾款" :visible.sync="payOrderVisible" :close-on-click-modal="false">
      <el-form :model="payOrderData" label-width="80px"  ref="orderForm">
        <el-form-item label="商机名字" >
          <el-input v-model="payOrderData.businessName" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品名字" >
          <el-input v-model="payOrderData.productName" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品价格" >
          <el-input v-model="payOrderData.price" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="顾客姓名">
          <el-input v-model="payOrderData.customerName" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="支付方式">
          <el-select v-model="payOrderData.payMethod" clearable value-key="id" placeholder="请选择">
            <el-option
                v-for="item in payModelList"
                :key="item.id"
                :label="item.title"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="payOrderVisible = false">取消</el-button>
        <el-button type="primary" @click.native="orderPaySubmit" :loading="orderFormLoading">提交</el-button>
      </div>
    </el-dialog>


	</section>
</template>

<script>
	export default {
		data() {
			return {
				query:{
				  currentPage:1,
          pageSize:10,
          keyword:""
        },
        pageInfo:{
          rows: [],
          total: 0,
        },
				listLoading: false,
        sels: [],//列表选中列
        selectValue:[],
        rows:[],
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        activitys:[],
        employees:[],
				orderFormVisible: false,//新增修改框
				orderFormLoading: false,
        orderAssignVisible:false,
        //支付尾款
        payOrderVisible:false,
        payOrderData:{
          orderId:null,
          productName:"",
          businessName:"",
          price:null,
          customerName:"",
          payMethod:{
            id:null,
            title:"",
            value:""
          }
        },
        payModelList:[],


        productList:[],
				orderFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				orderForm: {
					id:null,
          fullName:"",
          company:"",
          job:"",
          email:"",
          phone:"",
          address:"",
          seller:{
					  id:null
          }
				},
        orderActivityVisible:false,
        orderActivity:{
				  orderId:null,
          activityIds:[]
        }
			}
		},
		methods: {
      orderPaySubmit(){
        console.log(this.payOrderData)
        this.$http.post("order/pay",this.payOrderData)
            .then(result =>{
              result = result.data
              if(result.success){
                this.payOrderVisible=false;
                this.$message({
                  message: '支付尾款成功'+result.message,
                  type: 'success'
                });
                this.query.currentPage = 1
                this.getOrders();
              }else{
                this.$message({
                  message: '支付尾款成功'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '支付尾款成功',
            type: 'success'
          });
        })
      },
      handlePayOrder(index,row){
        this.payOrderData = {
          orderId:row.id,
          productName: row.productName,
          businessName: row.businessName,
          price: row.price,
          customerName: row.customerName,
          payMethod: {
            id: null
          }
        }
        this.payOrderVisible = true;
        this.$http.get("/dictionary/item/payModel")
            .then(result =>{
              result = result.data;
              if (result.success){
                this.payModelList = result.result;
              }else {
                this.$message({
                  message: '获取支付方式失败'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '获取支付方式失败',
            type: 'error'
          });
        })
      },
      changeBusinessSubmit(){
          this.$http.put("/order/business",this.businessData)
              .then(result =>{
                result = result.data
                if(result.success){
                  this.businessVisible=false;
                  this.$message({
                    message: '商机转化成功'+result.message,
                    type: 'success'
                  });
                  this.query.currentPage = 1
                  this.getOrders();
                }else{
                  this.$message({
                    message: '商机转化失败'+result.message,
                    type: 'error'
                  });
                }
              }).catch(result =>{
            this.$message({
              message: '商机转化失败',
              type: 'error'
            });
          })
      },
      handleChangeBusiness(){
        if (this.sels.length != 1){
          this.$message({
            message: '请选择一条线索在分配',
            type: 'error'});
          return ;
        }
        this.businessData.orderForm = this.sels[0];
        this.businessVisible = true;
        this.$http.get("/product")
            .then(result =>{
              result = result.data;
              if (result.success){
                this.productList = result.result;
              }else {
                this.$message({
                  message: '获取产品失败'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '获取产品失败',
            type: 'error'
          });
        })
      },
		  //线索分配提交
      orderAssignSubmit(){
        this.$http.post("order/assign",this.orderForm)
            .then(result =>{
              result = result.data
              if(result.success){
                this.orderAssignVisible=false;
                this.$message({
                  message: '线索分配成功'+result.message,
                  type: 'success'
                });
                this.query.currentPage = 1
                this.getOrders();
              }else{
                this.$message({
                  message: '线索分配失败'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '线索分配失败',
            type: 'error'
          });
        })
      },
      handleAssign(){
        if (this.sels.length != 1){
          this.$message({
            message: '请选择一条线索在分配',
            type: 'error'});
          return ;
        }
        this.orderForm = this.sels[0];
        this.orderAssignVisible = true;
        this.getEmployees();
      },
      getEmployees(){
        this.$http.get("/employee")
            .then(result =>{
              result = result.data;
              if (result.success){
                this.employees = result.result;
              }else {
                this.$message({
                  message: '获取员工列表失败'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '获取员工列表失败',
            type: 'error'
          });
        })
      },
		  orderActivitySubmit(){
        this.$http.post("order/activity",this.orderActivity)
                .then(result =>{
                  result = result.data
                  if(result.success){
                    this.orderActivityVisible=false;
                    this.$message({
                      message: '添加活动成功'+result.message,
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: '添加活动失败'+result.message,
                      type: 'error'
                    });
                  }
                }).catch(result =>{
          this.$message({
            message: '添加活动失败',
            type: 'error'
          });
        })
      },
      getActivitys(){
        this.$http.get("/activity/legal")
            .then(result =>{
              result = result.data;
              if (result.success){
                this.activitys = result.result;
              }else {
                this.$message({
                  message: '获取活动列表失败'+result.message,
                  type: 'error'
                });
              }
            }).catch(result =>{
          this.$message({
            message: '获取活动列表失败',
            type: 'error'
          });
        })
      },
      handleOrderActivity(index,row){
        this.orderActivityVisible = true;
        this.orderActivity.orderId = row.id;
        this.getActivitys();
      },
		  //高级查询
      search(){
        this.query.currentPage=1;
        this.getOrders();
      },
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getOrders();
			},
			//获取部门列表
			getOrders() {
				this.listLoading = true;//加载框
        this.$http.post("/order",this.query)
                  .then(result =>{
                    result = result.data
                    console.log(result)
                    if(result.success){
                      this.pageInfo = result.result
                      this.listLoading = false //关闭加载框
                    }else{
                      this.$message({
                        message: '查询失败'+result.message,
                        type: 'error'
                      });
                    }
                  }).catch(result =>{
                      this.$message({
                        message: '查询失败',
                        type: 'error'
                      });
                  })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/order/"+row.id)
                    .then(result =>{
                      result =result.data;
                      if (result.success){
                        this.$message({
                          message: '删除成功',
                          type: 'success'
                        });
                          //删除成功会到第一页查询
                          this.query.currentPage = 1;
                          this.getOrders();
                          this.listLoading = false //关闭弹出框
                      }else{
                        this.$message({
                          message: '删除失败'+result.message,
                          type: 'error'
                        });
                      }
                    })
				}).catch(() => {
          this.$message({
            message: '已取消删除',
            type: 'error'
          });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.orderFormVisible = true;
				this.orderForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.orderFormVisible = true;
				this.orderForm = {
          id:null,
          name:"",
          manager:{
            id:null
          },
          parent:{
            id:null
          },
          path:"",
          state:"",
          intro:""
				};
        this.getEmployees();
        this.getDeptTree();
			},

			//新增/修改保存
			save: function () {
				this.$refs.orderForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.orderFormLoading = true;
							let para = Object.assign({}, this.orderForm);
							this.$http.put("/order",para)
                  .then(result =>{
                    result =result.data;
                    if (result.success){
                      this.$message({
                        message: '保存成功',
                        type: 'success'
                      });
                      this.orderFormLoading = false;
                      this.query.currentPage = 1;
                      this.getOrders();
                      this.orderFormVisible = false;
                    }else {
                      this.$message({
                        message: '保存失败'+result.message,
                        type: 'error'
                      });
                    }
                  }).catch(result =>{
                        this.$message({
                          message: '保存失败',
                          type: 'error'
                        });
              })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchDelete: function () {
        //获取所有要删除行的id
        let ids = this.selectValue.map(orderForm=>orderForm.id);
        console.log(ids);
        //判断用户是否勾选了数据,未勾选给他一个提示框并返回
        if (ids.length<1){
          this.$message({
            message: '请选择数据后在删除',
            type: 'error'
          });
          return ;
        }
        //如果勾选了就弹出确认框
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
         let data = {
            ids:ids
          }
          this.$http.patch("/order",data.ids)
              .then(result =>{
                result =result.data;
                if (result.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                  //删除成功会到第一页查询
                  this.query.currentPage = 1;
                  this.getOrders();
                  this.listLoading = false //关闭弹出框
                }else{
                  this.$message({
                    message: '删除失败'+result.message,
                    type: 'error'
                  });
                }
              })
        }).catch(() => {
          this.$message({
            message: '已取消删除',
            type: 'error'
          });
        });


				/*var ids = this.rows.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getOrders();
					});
				}).catch(() => {
				});*/

				//结束
			}
		},
		mounted() {
		  //分页查询
			this.getOrders();
		}
	}

</script>

<style scoped>

</style>